package com.sixth.controller;

import com.sixth.bean.MarketCoupon;
import com.sixth.bean.MarketCouponUser;
import com.sixth.bean.bo.*;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.AddCouponVO;
import com.sixth.bean.vo.BaseListVO;
import com.sixth.bean.vo.CouponVO;
import com.sixth.constant.Constant;
import com.sixth.service.CouponService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Chen
 * @Date 2022-09-07  19-39-23
 * @description 优惠券管理类
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;

    /**
     * @Author Chen
     * @Date 2022-09-07  16-46-58
     * @param
     * @return {@link null }
     * @description 查询优惠劵
     */
    @RequiresPermissions(value = {"admin:coupon:list"},logical = Logical.OR)
    @GetMapping("list")
    public BaseRespVo getCouponList(CouponPageInfo couponPageInfo){

        BaseListVO<MarketCoupon> baseListVO = couponService.getCouponList(couponPageInfo);

        if (baseListVO.getList().size()>= Constant.ONE){
            return BaseRespVo.ok(baseListVO);
        }

        return BaseRespVo.ok("暂无数据");
    }

    /**
     * @Author Chen
     * @Date 2022-09-07  19-38-42
     * @param
     * @return {@link null }
     * @description 修改优惠券
     */
    @RequiresPermissions(value = {"admin:coupon:update"},logical = Logical.OR)
    @PostMapping("update")
    public BaseRespVo updateCoupon(@RequestBody CouponBO couponBO){

        MarketCoupon coupon = couponService.updateCoupon(couponBO);

        if (coupon!=null){
            return BaseRespVo.ok(coupon);
        }

        return BaseRespVo.invalidParameter("修改失败");
    }

    /**
     * @Author Chen
     * @Date 2022-09-07  17-45-26
     * @param
     * @return {@link null }
     * @description 根据ID获取优惠券详情
     */
    @RequiresPermissions(value = {"admin:coupon:read"},logical = Logical.OR)
    @GetMapping("read")
    public BaseRespVo readCouponDetail(Integer id){

        CouponVO couponVO = couponService.getCouponDetail(id);

        if (couponVO == null){
            return BaseRespVo.invalidParameter("暂无优惠券");
        }

        return BaseRespVo.ok(couponVO);
    }

    /**
     * @Author Chen
     * @Date 2022-09-07  22-04-07
     * @param
     * @return {@link null }
     * @description 对优惠券做逻辑删除
     */
    @RequiresPermissions(value = {"admin:coupon:listuser"},logical = Logical.OR)
    @GetMapping("listuser")
    public BaseRespVo couponListUser(CouponUserPage couponUserPage){

        BaseListVO<MarketCouponUser> couponUserListVO = couponService.getCouponListUser(couponUserPage);

        if (couponUserListVO.getList().size()>=Constant.ONE){
            return BaseRespVo.ok(couponUserListVO);

        }

        return BaseRespVo.invalidParameter("暂无数据");
    }


    /**
     * @Author Chen
     * @Date 2022-09-07  22-20-08
     * @param
     * @return {@link null }
     * @description 逻辑删除优惠券
     */
    @RequiresPermissions(value = {"admin:coupon:delete"},logical = Logical.OR)
    @PostMapping("delete")
    public BaseRespVo deleteCoupon(@RequestBody CouponDelBO couponDelBO){

        int delCode = couponService.delCoupon(couponDelBO);

        if (delCode == Constant.OK){
            return BaseRespVo.ok("删除成功",Constant.ZERO);
        }

        return BaseRespVo.invalidParameter("删除失败");
    }


    @RequiresPermissions(value = {"admin:coupon:create"},logical = Logical.OR)
    @PostMapping("create")
    public BaseRespVo createCoupon(@RequestBody AddCouponBO addCouponBO){

        AddCouponVO addCouponVO = couponService.addCoupon(addCouponBO);

        if (addCouponVO == null){
            return BaseRespVo.invalidParameter("添加失败");
        }

        return BaseRespVo.ok(addCouponVO);
    }
}
